from flask import render_template, request, redirect
from app_db import *
from choose_fund_eastmoney import eastmoney_run
from choose_fund_sina import sina_run


@app.route('/')
def read_index():
    return redirect(url_for('index'))


# index
@app.route('/index', methods=['GET', 'POST'])
def index():
    codes = ''

    if request.method == 'POST':
        peroid_list = request.form.getlist('peroid')
        top_num = request.form.get('top_num')
        type = request.form.get('type_id')
        codes = eastmoney_run(type=type,
                              peroid_list=peroid_list,
                              top_num=top_num)

    return render_template('index.html', eastmoney_data=codes, sina_data='')


@app.route('/info', methods=['GET', 'POST'])
def sina():
    codes = ''

    if request.method == 'POST':
        peroid_list = request.form.getlist('peroid')
        top_num = request.form.get('top_num')
        type = request.form.get('type_id')
        codes = sina_run(type=type,
                         peroid_list=peroid_list,
                         top_num=top_num)
    return render_template('index.html', eastmoney_data='', sina_data=codes)
